package br.com.appestoque.protocolo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import br.com.appestoque.util.Constantes;

public class Http {

	public static void requisicao(String URL, List<NameValuePair> parametros) {
		
		HttpClient httpClient = new DefaultHttpClient();
		try{
			HttpPost httpPost = new HttpPost(URL);
			httpPost.setEntity(new UrlEncodedFormEntity(parametros,HTTP.UTF_8));
			httpPost.setHeader("Accept-Encoding", "gzip");
			httpClient.execute(httpPost);
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			httpClient.getConnectionManager().shutdown();
		}
	}

	
	public static void status(String url) throws Exception {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader("Accept-Encoding", "gzip");
		HttpResponse httpResponse = httpClient.execute(httpPost);
		int statusCode = httpResponse.getStatusLine().getStatusCode();
		httpPost.abort();
		httpClient.getConnectionManager().shutdown();
		switch (statusCode) {
			case HttpStatus.SC_ACCEPTED:
				throw new Exception("202 Accepted (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_BAD_GATEWAY:
				throw new Exception("502 Bad Gateway (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_BAD_REQUEST:
				throw new Exception("400 Bad Request (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_CONFLICT:
				throw new Exception("409 Conflict (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_CONTINUE:
				throw new Exception("100 Continue (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_CREATED:
				throw new Exception("201 Created (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_EXPECTATION_FAILED:
				throw new Exception("417 Expectation Failed (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_FAILED_DEPENDENCY:
				throw new Exception("424 Failed Dependency (WebDAV - RFC 2518)");
			case HttpStatus.SC_FORBIDDEN:
				throw new Exception("403 Forbidden (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_GATEWAY_TIMEOUT:
				throw new Exception("504 Gateway Timeout (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_GONE:
				throw new Exception("410 Gone (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
				throw new Exception(
						"505 HTTP Version Not Supported (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
				throw new Exception("Static constant for a 419 error.");
			case HttpStatus.SC_INSUFFICIENT_STORAGE:
				throw new Exception("507 Insufficient Storage (WebDAV - RFC 2518)");
			case HttpStatus.SC_INTERNAL_SERVER_ERROR:
				throw new Exception("500 Server Error (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_LENGTH_REQUIRED:
				throw new Exception("411 Length Required (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_LOCKED:
				throw new Exception("423 Locked (WebDAV - RFC 2518)");
			case HttpStatus.SC_METHOD_FAILURE:
				throw new Exception("Static constant for a 420 error.");
			case HttpStatus.SC_METHOD_NOT_ALLOWED:
				throw new Exception("405 Method Not Allowed (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_MOVED_PERMANENTLY:
				throw new Exception("301 Moved Permanently (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_MOVED_TEMPORARILY:
				throw new Exception(
						"302 Moved Temporarily (Sometimes Found) (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_MULTIPLE_CHOICES:
				throw new Exception("300 Mutliple Choices (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_MULTI_STATUS:
				throw new Exception(
						"207 Multi-Status (WebDAV - RFC 2518) or 207 Partial Update OK (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)");
			case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
				throw new Exception(
						"203 Non Authoritative Information (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_NOT_ACCEPTABLE:
				throw new Exception("406 Not Acceptable (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_NOT_FOUND:
				throw new Exception("404 Not Found (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_NOT_IMPLEMENTED:
				throw new Exception("501 Not Implemented (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_NOT_MODIFIED:
				throw new Exception("304 Not Modified (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_NO_CONTENT:
				throw new Exception("204 No Content (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_OK:
				System.out.println(new SimpleDateFormat(Constantes.FORMATO_DATA_HORA_PADRAO).format(new Date()) + " - 200 OK (HTTP/1.0 - RFC 1945)");
				break;
			case HttpStatus.SC_PARTIAL_CONTENT:
				throw new Exception("206 Partial Content (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_PAYMENT_REQUIRED:
				throw new Exception("402 Payment Required (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_PRECONDITION_FAILED:
				throw new Exception("412 Precondition Failed (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_PROCESSING:
				throw new Exception("102 Processing (WebDAV - RFC 2518)");
			case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
				throw new Exception(
						"407 Proxy Authentication Required (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
				throw new Exception(
						"416 Requested Range Not Satisfiable (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_REQUEST_TIMEOUT:
				throw new Exception("408 Request Timeout (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_REQUEST_TOO_LONG:
				throw new Exception(
						"413 Request Entity Too Large (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_REQUEST_URI_TOO_LONG:
				throw new Exception(
						"414 Request-URI Too Long (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_RESET_CONTENT:
				throw new Exception("205 Reset Content (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_SEE_OTHER:
				throw new Exception("303 See Other (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_SERVICE_UNAVAILABLE:
				throw new Exception("503 Service Unavailable (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_SWITCHING_PROTOCOLS:
				throw new Exception("101 Switching Protocols (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_TEMPORARY_REDIRECT:
				throw new Exception("307 Temporary Redirect (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_UNAUTHORIZED:
				throw new Exception("401 Unauthorized (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_UNPROCESSABLE_ENTITY:
				throw new Exception("422 Unprocessable Entity (WebDAV - RFC 2518)");
			case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
				throw new Exception(
						"415 Unsupported Media Type (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_USE_PROXY:
				throw new Exception("305 Use Proxy (HTTP/1.1 - RFC 2616)");
			default:
				throw new Exception("RETORNO DESCONHECIDO");
		}
	}

	public static void enviar(String url, JSONObject objetos) throws Exception {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		httpPost.setHeader("Accept-Encoding", "gzip");
		StringEntity stringEntity = new StringEntity(objetos.toString(),HTTP.UTF_8);
		stringEntity.setContentType("application/json;charset=utf-8");
		
		httpPost.setEntity(stringEntity);
		
		HttpResponse httpResponse = httpClient.execute(httpPost);
		
		int statusCode = httpResponse.getStatusLine().getStatusCode();
		httpPost.abort();
		httpClient.getConnectionManager().shutdown();
		switch (statusCode) {
			case HttpStatus.SC_ACCEPTED:
				throw new Exception("202 Accepted (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_BAD_GATEWAY:
				throw new Exception("502 Bad Gateway (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_BAD_REQUEST:
				throw new Exception("400 Bad Request (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_CONFLICT:
				throw new Exception("409 Conflict (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_CONTINUE:
				throw new Exception("100 Continue (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_CREATED:
				throw new Exception("201 Created (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_EXPECTATION_FAILED:
				throw new Exception("417 Expectation Failed (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_FAILED_DEPENDENCY:
				throw new Exception("424 Failed Dependency (WebDAV - RFC 2518)");
			case HttpStatus.SC_FORBIDDEN:
				throw new Exception("403 Forbidden (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_GATEWAY_TIMEOUT:
				throw new Exception("504 Gateway Timeout (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_GONE:
				throw new Exception("410 Gone (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
				throw new Exception(
						"505 HTTP Version Not Supported (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
				throw new Exception("Static constant for a 419 error.");
			case HttpStatus.SC_INSUFFICIENT_STORAGE:
				throw new Exception("507 Insufficient Storage (WebDAV - RFC 2518)");
			case HttpStatus.SC_INTERNAL_SERVER_ERROR:
				throw new Exception("500 Server Error (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_LENGTH_REQUIRED:
				throw new Exception("411 Length Required (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_LOCKED:
				throw new Exception("423 Locked (WebDAV - RFC 2518)");
			case HttpStatus.SC_METHOD_FAILURE:
				throw new Exception("Static constant for a 420 error.");
			case HttpStatus.SC_METHOD_NOT_ALLOWED:
				throw new Exception("405 Method Not Allowed (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_MOVED_PERMANENTLY:
				throw new Exception("301 Moved Permanently (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_MOVED_TEMPORARILY:
				throw new Exception(
						"302 Moved Temporarily (Sometimes Found) (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_MULTIPLE_CHOICES:
				throw new Exception("300 Mutliple Choices (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_MULTI_STATUS:
				throw new Exception(
						"207 Multi-Status (WebDAV - RFC 2518) or 207 Partial Update OK (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)");
			case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
				throw new Exception(
						"203 Non Authoritative Information (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_NOT_ACCEPTABLE:
				throw new Exception("406 Not Acceptable (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_NOT_FOUND:
				throw new Exception("404 Not Found (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_NOT_IMPLEMENTED:
				throw new Exception("501 Not Implemented (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_NOT_MODIFIED:
				throw new Exception("304 Not Modified (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_NO_CONTENT:
				throw new Exception("204 No Content (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_OK:
				System.out.println(new SimpleDateFormat(Constantes.FORMATO_DATA_HORA_PADRAO).format(new Date()) + " - 200 OK (HTTP/1.0 - RFC 1945)");
				break;
			case HttpStatus.SC_PARTIAL_CONTENT:
				throw new Exception("206 Partial Content (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_PAYMENT_REQUIRED:
				throw new Exception("402 Payment Required (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_PRECONDITION_FAILED:
				throw new Exception("412 Precondition Failed (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_PROCESSING:
				throw new Exception("102 Processing (WebDAV - RFC 2518)");
			case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
				throw new Exception(
						"407 Proxy Authentication Required (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
				throw new Exception(
						"416 Requested Range Not Satisfiable (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_REQUEST_TIMEOUT:
				throw new Exception("408 Request Timeout (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_REQUEST_TOO_LONG:
				throw new Exception(
						"413 Request Entity Too Large (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_REQUEST_URI_TOO_LONG:
				throw new Exception(
						"414 Request-URI Too Long (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_RESET_CONTENT:
				throw new Exception("205 Reset Content (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_SEE_OTHER:
				throw new Exception("303 See Other (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_SERVICE_UNAVAILABLE:
				throw new Exception("503 Service Unavailable (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_SWITCHING_PROTOCOLS:
				throw new Exception("101 Switching Protocols (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_TEMPORARY_REDIRECT:
				throw new Exception("307 Temporary Redirect (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_UNAUTHORIZED:
				throw new Exception("401 Unauthorized (HTTP/1.0 - RFC 1945)");
			case HttpStatus.SC_UNPROCESSABLE_ENTITY:
				throw new Exception("422 Unprocessable Entity (WebDAV - RFC 2518)");
			case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
				throw new Exception(
						"415 Unsupported Media Type (HTTP/1.1 - RFC 2616)");
			case HttpStatus.SC_USE_PROXY:
				throw new Exception("305 Use Proxy (HTTP/1.1 - RFC 2616)");
			default:
				throw new Exception("RETORNO DESCONHECIDO");
		}
	}
	
}